#include "StdAfx.h"

#include "VCNetHelper.h"
#include "ustringutils.h"

USING_LIBSPACE

std::string CastHelper::Cast_std_string(System::String ^ s)
{
	const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
	std::string os = chars;
	Marshal::FreeHGlobal(IntPtr((void*)chars));

	return os;
}

std::wstring CastHelper::Cast_std_wstring (System::String ^ s)
{
	const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
	std::wstring os = chars;
	Marshal::FreeHGlobal(IntPtr((void*)chars));

	return os;
}


System::String^ CastHelper::Cast_String(const char* s)
{
	System::String^ ret = Marshal::PtrToStringAnsi(IntPtr((void*)s));
	return ret;
}

System::String^ CastHelper::Cast_String(const wchar_t* s)
{
	System::String^ ret = Marshal:: PtrToStringUni(IntPtr((void*)s));
	return ret;
}

System::String^ CastHelper::Cast_String(const std::string& s)
{
	return Cast_String(s.c_str());
}

System::String^ CastHelper::Cast_String(const std::wstring& s)
{
	return Cast_String(s.c_str());
}

Dictionary<System::String^,System::String^>^ CastHelper::ToNetMap( const std::map<tstring,tstring>& _map )
{
	Dictionary<System::String^,System::String^>^ ret = gcnew Dictionary<System::String^,System::String^>();

	for(std::map<tstring,tstring>::const_iterator it = _map.begin(); it!= _map.end(); ++it)
	{
		System::String^ key = gcnew System::String (it->first.c_str());
		System::String^ val = gcnew System::String (it->second.c_str());
		ret[key] = val;
	}
	return ret;
}

std::map<tstring,tstring> CastHelper::ToMap( Dictionary<System::String^,System::String^>^ input )
{
	std::map<tstring,tstring> ret;



	for each(KeyValuePair<System::String^, System::String^>^ item in input)
	{
		const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(item->Key)).ToPointer();
		std::wstring os = chars;
		Marshal::FreeHGlobal(System::IntPtr((void*)chars));


		const wchar_t* chars1 = (const wchar_t*)(Marshal::StringToHGlobalUni(item->Value)).ToPointer();
		std::wstring os1 = chars1;
		Marshal::FreeHGlobal(System::IntPtr((void*)chars1));

		tstring key = UStrConvUtils::ToTS(os);
		tstring val = UStrConvUtils::ToTS(os1);


		ret[key] = val;
	}
	return ret;
}
